package com.nativetalk.controller.app.two.feedback;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.feedback.TdNtFeedbackInsert;
import com.nativetalk.bean.feedback.TdNtFeedbackTeaInsert;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.feedback.insert.FeedbackInsertService;
import com.nativetalk.service.feedback.search.FeedbackSearchService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class TwoFeedbackController {
    private static Logger logger = Logger.getLogger(TwoFeedbackController.class);
    @Resource
    private FeedbackSearchService feedbackSearchService;
    @Resource
    private FeedbackInsertService feedbackInsertService;

    /**
     * @return RetInfo
     * @Purpose 查找教师消息记录
     * @version 2.1
     * @time 2015-05-19
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/feedback/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo list(@RequestBody Page page,
                        @RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("token is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            page.setObj(tdNtTeacherResult.getTea_id());
            RetInfo retInfo = feedbackSearchService.findFeedbackList(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查找教师消息记录详情
     * @version 2.1
     * @time 2015-05-19
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/feedback/list/detail", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo detail(@RequestBody Page page) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            retInfo = feedbackSearchService.findFeedbackDetailList(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 教师添加意见反馈
     * @version 1.0
     * @time 2015-01-09
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/teatoken/feedback/add", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teacher(@Valid @RequestBody TdNtFeedbackTeaInsert tdNtFeedbackTeaInsert, BindingResult result,
                           @RequestHeader(value = "token", required = true) String token) {

        if (result.hasErrors()) {
            RetInfo retInfo = new RetInfo();
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
            retInfo.setTip(fe.getDefaultMessage());
            return retInfo;
        }

        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtFeedbackTeaInsert));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("token is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            TdNtFeedbackInsert tdNtFeedbackInsert = new TdNtFeedbackInsert();
            tdNtFeedbackInsert.setFeed_content(tdNtFeedbackTeaInsert.getFeed_content());
            tdNtFeedbackInsert.setUser_id(tdNtTeacherResult.getTea_id());
            tdNtFeedbackInsert.setUser_cate("2");
            tdNtFeedbackInsert.setParent_id(0);

            RetInfo retInfo = feedbackInsertService.addFeedback(tdNtFeedbackInsert);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            RetInfo retInfo = new RetInfo();
            //记录错误日志
            retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
}


